package com.leo.manage_system.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leo.manage_system.entity.Res;
import com.leo.manage_system.entity.vo.ResQueryVo;
import com.leo.manage_system.service.ResService;
import com.leo.manage_system.utils.JwtUtils;
import com.leo.manage_system.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 物品表 前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-11-01
 */
@RestController
@RequestMapping("/service_manage/res")
@CrossOrigin
@Api(tags = "物品管理")
public class ResController {
    JwtUtils jwtUtils = new JwtUtils();
    final ResService resService;

    @Autowired
    public ResController(ResService resService){
        this.resService=resService;


    }
    @ApiOperation(value = "分页物品列表")
    @PostMapping("ResPageQuery/{page}/{limit}")
    public R getResPageQuery(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit,
            @ApiParam(name = "resQuery", value = "查询物品对象", required = false)
            @RequestBody(required = false) ResQueryVo resQuery
            ){
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            Page<Res> pageParam = new Page<>(page, limit);
            return R.ok().data(resService.getResPageQuery(pageParam,resQuery));
        }
    }

    @ApiOperation("发布物品")
    @PostMapping("/addRes")
    public R addRes(@ApiParam(name = "token", value = "token", required = true)
                        @RequestParam(value="token") String token,
                    @ApiParam("物品") @RequestBody Res res
//                    @ApiParam("用户ID") @RequestParam String user_id,
                    ) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean save = resService.save(res);
            if (save) return R.ok().message("添加成功");
            return R.error().message("添加失败");
        }

    }

    @ApiOperation("删除物品")
    @DeleteMapping("/deleteRes")
    public R deleteRes(@ApiParam(name = "token", value = "token", required = true)
                           @RequestParam(value="token") String token,
                       @ApiParam(name ="resId",value = "物品ID" ,required = true) @RequestParam(value="resId") String resId
//                       @ApiParam("用户ID") @RequestParam String user_id
    ) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            //        Res byId = resService.getById(resId);
//        if (!byId.getUserId().equals(user_id)) return R.error().message("该物品不属于该用户");
            boolean b = resService.removeById(resId);
            if (b) return R.ok().message("删除成功");
            return R.error().message("删除失败");
        }

    }
    @ApiOperation("编辑物品")
    @PostMapping("/modifyRes")
    public R modifyRes(
            @ApiParam(name = "token", value = "token", required = true)
            @RequestParam(value="token") String token,
            @ApiParam("物品") @RequestBody Res res) {
        if(jwtUtils.verify(token)==2){
            return R.error().message("token已过期");
        }
        else if(jwtUtils.verify(token)==0){
            return R.error().message("token认证失败");
        }
        else {
            boolean b = resService.updateById(res);
            if (b) return R.ok().message("编辑成功");
            return R.error().message("编辑失败");
        }

    }
//    @ApiOperation("获取物品对象")
//    @GetMapping("/getRes")
//    public R getRes(@ApiParam("物品ID") @RequestParam String res_id) {
//        ResVo res = resService.getRes(res_id);
//        return R.ok().data("res", res);
//    }




}

